//
//  GWDiscoverCell.m
//  新浪微博
//
//  Created by qianfeng on 16/2/25.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWDiscoverCell.h"
#import "GWDiscoverItem.h"
#import "GWCommonIArrowtem.h"
#import "GWCommonSwitchItem.h"
#import "GWCommonLabelItem.h"
#import "GWBadgeView.h"
@interface GWDiscoverCell ()
@property (nonatomic,strong)UIImageView *rightArraw;
@property (nonatomic,strong)UISwitch *rightSwitch;
@property (nonatomic,strong)UILabel *rightLabel;
@property (nonatomic,strong)GWBadgeView *badgeView;
@end
@implementation GWDiscoverCell
-(UILabel *)rightLabel
{
    if (_rightLabel==nil) {
        _rightLabel =[[UILabel alloc]init];
        _rightLabel.textColor =[UIColor lightGrayColor];
        _rightLabel.font =[UIFont systemFontOfSize:13];
    }
    return _rightLabel;
}
-(GWBadgeView *)badgeView
{
    if (_badgeView ==nil) {
        _badgeView =[[GWBadgeView alloc]init];
    }
    return _badgeView;
}
-(UIView *)rightArraw
{
    if (_rightArraw ==nil) {
        _rightArraw =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_icon_arrow"]];
    }
    return _rightArraw;
}
-(UISwitch *)rightSwitch
{
    if (_rightSwitch ==nil) {
        _rightSwitch =[[UISwitch alloc]init];
    }
    return _rightSwitch;
}

+(instancetype)discoverCellWithTableView:(UITableView *)tableView
{
    static NSString *cellID =@"GWDiscoverCell";
    GWDiscoverCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell ==nil) {
        cell =[[GWDiscoverCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self =[super initWithStyle:style  reuseIdentifier:reuseIdentifier]) {
        
        self.textLabel.font =[UIFont boldSystemFontOfSize:15];
        self.detailTextLabel.font =[UIFont systemFontOfSize:12];
        self.selectedBackgroundView =[[UIImageView alloc]init];
        self.backgroundView =[[UIImageView alloc]init];
        self.backgroundColor =[UIColor clearColor];
    }
    return self;
}
-(void)setIndexPath:(NSIndexPath *)path andNumberOfRows:(int)rows
{
    UIImageView *bgView =(UIImageView *)self.backgroundView;
    UIImageView *selectedView =(UIImageView *)self.selectedBackgroundView;
    if (rows==1) {
        bgView.image =[UIImage reSizeWithName:@"common_card_top_background"];
        selectedView.image =[UIImage reSizeWithName:@"common_card_top_background_highlighted"];
      
    }
    else if (path.row==0)
    {
        bgView.image =[UIImage reSizeWithName:@"common_card_top_background"];
        selectedView.image =[UIImage reSizeWithName:@"common_card_top_background_highlighted"];
    }
    else if (path.row==rows-1)
    {
        bgView.image =[UIImage reSizeWithName:@"common_card_bottom_background"];
        selectedView.image =[UIImage reSizeWithName:@"common_card_middle_background_highlighted"];
    }
    else
    {
        bgView.image =[UIImage reSizeWithName:@"common_card_middle_background"];
        selectedView.image =[UIImage reSizeWithName:@"common_card_middle_background_highlighted"];
    }
    self.backgroundView =bgView;
    self.selectedBackgroundView =selectedView;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.detailTextLabel.x =CGRectGetMaxX(self.textLabel.frame)+5;
}
-(void)setItem:(GWDiscoverItem *)item
{
    
    _item =item;
    self.imageView.image =[UIImage imageNamed:item.icon];
    self.textLabel.text =item.title;
    self.detailTextLabel.text =item.subleTitle;
    if (item.bageValue) {
        self.accessoryView =self.badgeView;
        self.badgeView.badgeValue =item.bageValue;
    }
    else if ([item isKindOfClass:[GWCommonIArrowtem class]])
    {
        self.accessoryView =self.rightArraw;
    }
    else if ([item isKindOfClass:[GWCommonSwitchItem class]])
    {
        self.accessoryView =self.rightSwitch;
    }
    else if ([item isKindOfClass:[GWCommonLabelItem class]])
    {
        GWCommonLabelItem *labelItem =(GWCommonLabelItem *)item;
        self.accessoryView =self.rightLabel;
        self.rightLabel.text =labelItem.labelText;
       self.rightLabel.size = [labelItem.labelText boundingRectWithSize:CGSizeMake(500, 200) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.rightLabel.font} context:0].size;
    }
    else
    {
        self.accessoryView =nil;
    }
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
